﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApFlexCableController
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Flexibles;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands;

public class CApFlexCableController : ICApFlexCableController
{
  private readonly CApFlexParameterController m_parameterController;
  private readonly ICApFLEXCableAttributeViewModel m_attributeViewModel;

  public CApFlexCableController(
    CApFlexParameterController parameterController,
    ICApFLEXCableAttributeViewModel attributeViewModel)
  {
    this.m_parameterController = parameterController;
    this.m_attributeViewModel = attributeViewModel;
  }

  public void InitViewModel()
  {
    this.m_attributeViewModel.IsCableEditable = true;
    this.m_attributeViewModel.CableName = new CApFlexCableNameGenerator().GenerateNewCableName();
    this.m_attributeViewModel.StartFrame = (ITxLocatableObject) null;
    this.m_attributeViewModel.EndFrame = (ITxLocatableObject) null;
    this.m_attributeViewModel.PassNearFrame = (ITxLocatableObject) null;
    this.m_attributeViewModel.Length = this.m_parameterController.Length;
    this.m_attributeViewModel.Density = this.m_parameterController.Density;
    this.m_attributeViewModel.YoungsModulus = this.m_parameterController.YoungsModulus;
    this.m_attributeViewModel.Radius = this.m_parameterController.Radius;
    this.m_attributeViewModel.MaxTension = this.m_parameterController.MaxTension;
    this.m_attributeViewModel.MaxTwistAngle = this.m_parameterController.MaxTwist;
    this.m_attributeViewModel.MinCurvatureRadius = this.m_parameterController.MinCurvatureRadius;
    this.m_attributeViewModel.MaxContactForce = this.m_parameterController.MaxContactForce;
    this.m_attributeViewModel.FulcrumLength = this.m_parameterController.FulcrumLength;
    this.m_attributeViewModel.Oscillation = this.m_parameterController.Oscillation;
    this.m_attributeViewModel.IsMaxTwistAngleLimited = this.m_parameterController.IsMaxTwistAngleLimited;
    this.m_attributeViewModel.IsMinCurvatureRadiusLimited = this.m_parameterController.IsMinCurvatureRadiusLimited;
    this.m_attributeViewModel.IsMaxContactForceLimited = this.m_parameterController.IsMaxContactForceLimited;
    this.InitializeExpanders();
    this.InitializeOptimizableParameters();
  }

  public void InitViewModelWithCable(ITxCable cable)
  {
    this.InitializeExpanders();
    this.InitializeOptimizableParameters();
    this.UpdateViewModelWithCable(cable);
  }

  public void UpdateViewModelWithCable(ITxCable cable)
  {
    this.m_attributeViewModel.IsCableEditable = true;
    this.m_attributeViewModel.IsBeingInitialized = true;
    this.m_attributeViewModel.CableName = ((ITxObject) cable).Name;
    try
    {
      TxCableData cableData = cable.CableData;
      this.m_attributeViewModel.Length = cableData.Length;
      this.m_attributeViewModel.Density = cableData.Density;
      this.m_attributeViewModel.YoungsModulus = cableData.YoungsModulus;
      this.m_attributeViewModel.Radius = cableData.Radius;
      this.m_attributeViewModel.FulcrumLength = cableData.FulcrumLength;
      this.m_attributeViewModel.Oscillation = cableData.Oscillation;
      this.m_attributeViewModel.StartFrame = cable.StartFrame;
      this.m_attributeViewModel.EndFrame = cable.EndFrame;
      this.m_attributeViewModel.StartAttachPartTo = (ITxObject) cable.StartFrame.AttachmentParent;
      this.m_attributeViewModel.EndAttachPartTo = (ITxObject) cable.EndFrame.AttachmentParent;
      this.m_attributeViewModel.PassNearFrame = cableData.PassNearFrame;
      this.m_attributeViewModel.MaxTension = cableData.MaximumAllowedTension;
      this.m_attributeViewModel.MaxTwistAngle = cableData.MaximumAllowedTwistRate;
      this.m_attributeViewModel.MinCurvatureRadius = cableData.MinimumAllowedCurvatureRadius;
      this.m_attributeViewModel.MaxContactForce = cableData.MaximumAllowedContactForce;
      this.m_attributeViewModel.IsMaxTwistAngleLimited = cableData.IsTwistRateLimited;
      this.m_attributeViewModel.IsMinCurvatureRadiusLimited = cableData.IsCurvatureRadiusLimited;
      this.m_attributeViewModel.IsMaxContactForceLimited = cableData.IsContactForceLimited;
      this.m_attributeViewModel.IsCableEditable = (cable as ITxComponent).IsOpenForModeling;
      this.InitAttachmentDataList(cable);
      this.m_attributeViewModel.IsBeingInitialized = false;
    }
    catch (Exception ex)
    {
      this.m_attributeViewModel.IsBeingInitialized = false;
      int num = (int) TxMessageBox.ShowModal(CApFLEXStringTable.CABLE_EDITION_FAIL, CApFLEXStringTable.CABLE_ERROR_DIALOG_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  private void InitAttachmentDataList(ITxCable cable)
  {
    if (cable == null)
      return;
    TxCableData cableData = cable.CableData;
    this.m_attributeViewModel.SetAttachmentList(CApFLEXAttachment.CreateAttachmentsFromCable(cable));
  }

  public bool AreCableDifferent(ITxCable cable, double epsilon)
  {
    TxCableData cableData = cable.CableData;
    bool flag1 = true;
    if (cable.StartFrame == this.m_attributeViewModel.StartFrame && cable.EndFrame == this.m_attributeViewModel.EndFrame && cableData.Length == this.m_attributeViewModel.Length && cableData.Density == this.m_attributeViewModel.Density && cableData.Radius == this.m_attributeViewModel.Radius && cableData.YoungsModulus == this.m_attributeViewModel.YoungsModulus && Math.Abs(cableData.MaximumAllowedTension - this.m_attributeViewModel.MaxTension) < 0.01 && Math.Abs(cableData.MaximumAllowedTwistRate - this.m_attributeViewModel.MaxTwistAngle) < epsilon && cableData.MinimumAllowedCurvatureRadius == this.m_attributeViewModel.MinCurvatureRadius && Math.Abs(cableData.MaximumAllowedContactForce - this.m_attributeViewModel.MaxContactForce) < epsilon && cableData.FulcrumLength == this.m_attributeViewModel.FulcrumLength && cableData.Oscillation == this.m_attributeViewModel.Oscillation && cableData.IsTwistRateLimited == this.m_attributeViewModel.IsMaxTwistAngleLimited && cableData.IsCurvatureRadiusLimited == this.m_attributeViewModel.IsMinCurvatureRadiusLimited && cableData.IsContactForceLimited == this.m_attributeViewModel.IsMaxContactForceLimited && cableData.PassNearFrame == this.m_attributeViewModel.PassNearFrame)
    {
      List<TxCableAttachment> attachments1 = this.GetAttachments();
      List<TxCableAttachment> attachments2 = cableData.Attachments;
      if (attachments2.Count == attachments1.Count)
      {
        bool flag2 = true;
        for (int index = 0; flag2 && index < attachments1.Count; ++index)
          flag2 = attachments2[index].Equals(attachments1[index]);
        flag1 = !flag2;
      }
    }
    return flag1;
  }

  public void SaveAndUpdateParameterValues()
  {
    this.m_parameterController.Length = this.m_attributeViewModel.Length;
    this.m_parameterController.Radius = this.m_attributeViewModel.Radius;
    this.m_parameterController.Density = this.m_attributeViewModel.Density;
    this.m_parameterController.YoungsModulus = this.m_attributeViewModel.YoungsModulus;
    this.m_parameterController.MaxTension = this.m_attributeViewModel.MaxTension;
    this.m_parameterController.MaxTwist = this.m_attributeViewModel.MaxTwistAngle;
    this.m_parameterController.MinCurvatureRadius = this.m_attributeViewModel.MinCurvatureRadius;
    this.m_parameterController.MaxContactForce = this.m_attributeViewModel.MaxContactForce;
    this.m_parameterController.FulcrumLength = this.m_attributeViewModel.FulcrumLength;
    this.m_parameterController.Oscillation = this.m_attributeViewModel.Oscillation;
    this.m_parameterController.IsLengthOptimizationActivated = this.m_attributeViewModel.IsLengthOptimizationActivated;
    this.m_parameterController.IsRadiusOptimizationActivated = this.m_attributeViewModel.IsRadiusOptimizationActivated;
    this.m_parameterController.IsYoungsModulusOptimizationActivated = this.m_attributeViewModel.IsYoungsModulusOptimizationActivated;
    this.m_parameterController.CreationMode = this.m_attributeViewModel.CreationMode;
    this.m_parameterController.IsParametersExpanderOpen = this.m_attributeViewModel.IsParametersExpanded;
    this.m_parameterController.IsAttachmentsExpanderOpen = this.m_attributeViewModel.IsAttachmentExpanded;
    this.m_parameterController.Save();
  }

  public ITxCable CreateCable()
  {
    TxTransformation relLoc1 = (TxTransformation) null;
    TxTransformation relLoc2 = (TxTransformation) null;
    CApCableUtility.ComputeCableMountRefData(this.m_attributeViewModel.StartFrame, this.m_attributeViewModel.StartAttachPartTo as ITxLocatableObject, ref relLoc1);
    CApCableUtility.ComputeCableMountRefData(this.m_attributeViewModel.EndFrame, this.m_attributeViewModel.EndAttachPartTo as ITxLocatableObject, ref relLoc2);
    TxCableCreationData cableCreationData = this.CreateCableCreationData(new TxCableData(relLoc1, this.m_attributeViewModel.StartAttachPartTo as ITxLocatableObject, relLoc2, this.m_attributeViewModel.EndAttachPartTo as ITxLocatableObject, this.m_attributeViewModel.Length, this.m_attributeViewModel.Radius)
    {
      YoungsModulus = this.GetYoungsModulus(),
      Density = this.m_attributeViewModel.Density,
      IsTwistRateLimited = this.m_attributeViewModel.IsMaxTwistAngleLimited,
      IsCurvatureRadiusLimited = this.m_attributeViewModel.IsMinCurvatureRadiusLimited,
      IsContactForceLimited = this.m_attributeViewModel.IsMaxContactForceLimited,
      MaximumAllowedTension = this.m_attributeViewModel.MaxTension,
      MaximumAllowedTwistRate = this.m_attributeViewModel.MaxTwistAngle,
      MaximumAllowedContactForce = this.m_attributeViewModel.MaxContactForce,
      MinimumAllowedCurvatureRadius = this.m_attributeViewModel.MinCurvatureRadius,
      FulcrumLength = this.m_attributeViewModel.FulcrumLength,
      Oscillation = this.m_attributeViewModel.Oscillation,
      PassNearFrame = this.GetPassNearFrame(),
      Attachments = this.GetAttachments()
    });
    TxPhysicalRoot physicalRoot = TxApplication.ActiveDocument.PhysicalRoot;
    ITxCable cable = (ITxCable) null;
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    bool flag = false;
    if (((Collection<ITxObject>) allItems).Count == 1 && ((Collection<ITxObject>) allItems)[0] is ITxCableCreation cableCreation && cableCreation.CanCreateCable(cableCreationData))
    {
      cable = this.CreateCable(cableCreation, cableCreationData);
      flag = true;
    }
    if (!flag)
      cable = this.CreateCable((ITxCableCreation) physicalRoot, cableCreationData);
    return cable;
  }

  public bool CanCreateOrUpdateCable()
  {
    bool orUpdateCable = this.m_attributeViewModel.IsLicenseOn;
    if (!orUpdateCable)
    {
      ITxCable cable = this.m_attributeViewModel.Cable;
      if (cable != null)
      {
        TxCableData cableData = this.m_attributeViewModel.Cable.CableData;
        this.UpdateCableData(cableData);
        orUpdateCable = TxCableUtilities.IsModificationAllowedWithoutLicense(cable, cableData);
      }
    }
    return orUpdateCable;
  }

  public bool UpdateCable(ref string errorMessage)
  {
    bool flag = true;
    errorMessage = string.Empty;
    ITxCable cable = this.m_attributeViewModel.Cable ?? throw new InvalidOperationException();
    ((ITxObject) cable).Name = this.m_attributeViewModel.CableName;
    TxCableData cableData1 = cable.CableData;
    TxCableData cableData2 = cable.CableData;
    this.UpdateCableData(cableData1);
    try
    {
      this.UpdateCable(cable, cableData1, this.m_attributeViewModel.ComputeOptimizationData());
    }
    catch (TxException ex)
    {
      errorMessage = CApCableExceptionMessageProvider.ComputeCableExceptionErrorMessage(ex, CApCableExceptionMessageProvider.CableAction.UPDATE);
      flag = false;
    }
    if (!flag)
    {
      try
      {
        this.UpdateCable(cable, cableData2);
      }
      catch (TxException ex)
      {
      }
    }
    return flag;
  }

  private double GetYoungsModulus()
  {
    double youngsModulus = 1.0;
    if (this.m_attributeViewModel.CreationMode != CUiFLEXCreationMode.FROM_POINT_CLOUD || !this.m_attributeViewModel.IsYoungsModulusOptimizationActivated)
      youngsModulus = this.m_attributeViewModel.YoungsModulus;
    return youngsModulus;
  }

  private ITxLocatableObject GetPassNearFrame()
  {
    return this.m_attributeViewModel.CreationMode != CUiFLEXCreationMode.MANUAL ? (ITxLocatableObject) null : this.m_attributeViewModel.PassNearFrame;
  }

  public List<TxCableAttachment> GetAttachments()
  {
    List<TxCableAttachment> attachments = new List<TxCableAttachment>();
    if (this.m_attributeViewModel.CreationMode == CUiFLEXCreationMode.MANUAL)
      attachments = this.m_attributeViewModel.RawAttachmentList().Select<CApFLEXAttachment, TxCableAttachment>((Func<CApFLEXAttachment, TxCableAttachment>) (a => a.Attachment)).ToList<TxCableAttachment>();
    return attachments;
  }

  private void InitializeExpanders()
  {
    this.m_attributeViewModel.IsParametersExpanded = this.m_parameterController.IsParametersExpanderOpen;
    this.m_attributeViewModel.IsAttachmentExpanded = this.m_parameterController.IsAttachmentsExpanderOpen;
  }

  private void InitializeOptimizableParameters()
  {
    this.m_attributeViewModel.IsYoungsModulusOptimizationActivated = this.m_parameterController.IsYoungsModulusOptimizationActivated;
    this.m_attributeViewModel.IsRadiusOptimizationActivated = this.m_parameterController.IsRadiusOptimizationActivated;
    this.m_attributeViewModel.IsLengthOptimizationActivated = this.m_parameterController.IsLengthOptimizationActivated;
  }

  private void UpdateCableData(TxCableData cableData)
  {
    cableData.Length = this.m_attributeViewModel.Length;
    cableData.Density = this.m_attributeViewModel.Density;
    cableData.Radius = this.m_attributeViewModel.Radius;
    cableData.YoungsModulus = this.GetYoungsModulus();
    cableData.MaximumAllowedTension = this.m_attributeViewModel.MaxTension;
    cableData.MaximumAllowedTwistRate = this.m_attributeViewModel.MaxTwistAngle;
    cableData.MinimumAllowedCurvatureRadius = this.m_attributeViewModel.MinCurvatureRadius;
    cableData.MaximumAllowedContactForce = this.m_attributeViewModel.MaxContactForce;
    cableData.FulcrumLength = this.m_attributeViewModel.FulcrumLength;
    cableData.Oscillation = this.m_attributeViewModel.Oscillation;
    cableData.IsTwistRateLimited = this.m_attributeViewModel.IsMaxTwistAngleLimited;
    cableData.IsCurvatureRadiusLimited = this.m_attributeViewModel.IsMinCurvatureRadiusLimited;
    cableData.IsContactForceLimited = this.m_attributeViewModel.IsMaxContactForceLimited;
    cableData.PassNearFrame = this.GetPassNearFrame();
    cableData.Attachments = this.GetAttachments();
    TxTransformation relLoc1 = (TxTransformation) null;
    TxTransformation relLoc2 = (TxTransformation) null;
    CApCableUtility.ComputeCableMountRefData(this.m_attributeViewModel.StartFrame, this.m_attributeViewModel.StartAttachPartTo as ITxLocatableObject, ref relLoc1);
    CApCableUtility.ComputeCableMountRefData(this.m_attributeViewModel.EndFrame, this.m_attributeViewModel.EndAttachPartTo as ITxLocatableObject, ref relLoc2);
    cableData.SetStart(relLoc1, this.m_attributeViewModel.StartAttachPartTo as ITxLocatableObject);
    cableData.SetEnd(relLoc2, this.m_attributeViewModel.EndAttachPartTo as ITxLocatableObject);
  }

  private TxCableCreationData CreateCableCreationData(TxCableData cableData)
  {
    TxCableCreationData cableCreationData = new TxCableCreationData(this.m_attributeViewModel.CableName, cableData);
    cableCreationData.SetOptimizationData(this.m_attributeViewModel.ComputeOptimizationData());
    return cableCreationData;
  }

  private ITxCable CreateCable(ITxCableCreation cableCreation, TxCableCreationData creationData)
  {
    ITxCable cable = cableCreation.CreateCable(creationData);
    this.UpdateViewModelWithCable(cable);
    return cable;
  }

  private void UpdateCable(
    ITxCable cable,
    TxCableData cableData,
    ITxCableOptimizationData optimizationData = null)
  {
    if (optimizationData != null && cable is TxCable txCable)
    {
      txCable.Optimize(cableData, optimizationData);
      this.UpdateViewModelWithCable(cable);
    }
    else
      cable.SetCableDataAndRegenerateCable(cableData);
    this.m_attributeViewModel.NotifyCableAttributeChanged();
  }
}
